package Offer;


/**
 * 从尾到头输出链表
 * @author 是阿秋啊
 * @date 2022/03/29 19:42
 **/
public class Num06 {
    public int[] reversePrint(ListNode head) {
        if (head == null) {
            return new int[0];
        }
        int size = 0;
        for (ListNode pre = head; pre != null; pre = pre.next ) {
            size++;
        }
        int[] ret = new int[size];
        helper(head,ret,size - 1);
        return ret;
    }

    private void helper(ListNode head, int[] ret,int index) {
        if (head.next == null) {
            ret[index] = head.val;
            return;
        }
        helper(head.next,ret,index - 1);
        ret[index] = head.val;
    }
}
